package br.android.photoeffect;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.GradientDrawable.Orientation;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;

public class AndroidFilterEffectsActivity extends Activity {
	/** Called when the activity is first created. */

	private static final int SELECT_PICTURE = 1;
	private static final int CAMERA_PIC_REQUEST = 2500;
	private boolean CAM = false;
	private boolean GALLERY = false;

	private String selectedImagePath;
	private ImageView mImageView;
	
	private static Bitmap sLastImage = null;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		mImageView = (ImageView) findViewById(R.id.image);

		((ImageView) findViewById(R.id.arquivo_image))
				.setOnClickListener(new OnClickListener() {

					public void onClick(View arg0) {
						GALLERY = true;
						CAM = false;
						Intent intent = new Intent();
						intent.setType("image/*");
						intent.setAction(Intent.ACTION_GET_CONTENT);
						startActivityForResult(
								Intent.createChooser(intent, "Select Picture"),
								SELECT_PICTURE);
					}
				});

		((ImageView) findViewById(R.id.cam_image))
				.setOnClickListener(new OnClickListener() {

					public void onClick(View arg0) {
						GALLERY = false;
						CAM = true;
						Intent cameraIntent = new Intent(
								android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
						startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST);

					}
				});
		
		if(sLastImage != null)
		{
			setImage(sLastImage);
		}

	}

	public void onActivityResult(int requestCode, int resultCode, Intent data) {

		Bitmap image = null;
		if (CAM) {
			if (requestCode == CAMERA_PIC_REQUEST) {
				image = (Bitmap) data.getExtras().get("data");
			}
		}
		if (GALLERY) {
			if (resultCode == RESULT_OK) {
				if (requestCode == SELECT_PICTURE) {
					Uri selectedImageUri = data.getData();
					selectedImagePath = getPath(selectedImageUri);
				}
				image = BitmapFactory.decodeFile(selectedImagePath);				
			}
		}
		
		setImage(image);
	}
	
	private void setImage(Bitmap image)
	{
		if(image != null){
			Drawable draw = null;
			
			if(mImageView.getWidth() > mImageView.getHeight()) {
				if(image.getWidth() > image.getHeight()) {
					draw = new BitmapDrawable(image);
				} else {
					Bitmap rot = rotate(image);	
					draw = new BitmapDrawable(rot);
				}
				
			} else {
				if(image.getWidth() > image.getHeight()) {
					Bitmap rot = rotate(image);
					draw = new BitmapDrawable(rot);
				} else {
					draw = new BitmapDrawable(image);
				}
			}
			
			if(draw != null)
			{
				sLastImage = image;
				mImageView.setImageDrawable(draw);
			}
			
		}
	}
	
	private Bitmap rotate(Bitmap bmp)
	{
		Matrix mat = new Matrix();
		mat.preRotate(90);///in degree
		int w = bmp.getWidth();
		int h = bmp.getHeight();
		Bitmap mBitmap = Bitmap.createBitmap(bmp, 0, 0, w, h, mat, true);
		return mBitmap;
	}

	public String getPath(Uri uri) {
		String[] projection = { MediaStore.Images.Media.DATA };
		Cursor cursor = managedQuery(uri, projection, null, null, null);
		int column_index = cursor
				.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
		cursor.moveToFirst();
		return cursor.getString(column_index);
	}

}